package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.PxAdversterConfigurationService;
import com.iot08.admin.service.PxAdversterService;
import com.iot08.admin.service.PxAdversterSlotService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.app.PxAdversterSlot;
import com.iot08.common.entity.app.PxAdvertConfiguration;
import com.iot08.common.response.Response;
import com.iot08.common.utils.StringUtil;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * @author cwj
 * @description
 * @date 2025/4/13 10:22
 **/
@Controller
@RequestMapping("pxAdversterConfiguration")
public class PxAdversterConfigurationController extends BaseController {


    @Resource
    private PxAdversterConfigurationService pxAdversterConfigurationService;

    @Resource
    private PxAdversterSlotService pxAdversterSlotService;

    @Resource
    private PxAdversterService pxAdversterService;
    /**
     * @description 查询广告配置
     * @author cwj
     * @param [page]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/13 10:32
     **/
    @RequestMapping("list")
    public ModelAndView list(PageVO page) {
        ModelAndView mv = new ModelAndView();
        Page<PxAdvertConfiguration> pages = pxAdversterConfigurationService.list(page);
        mv.addObject("adversterConfigurations", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("adversters/px_advert_configuration_list");
        return mv;
    }


    /**
     * @description 显示广告配置页面
     * @author cwj
     * @param []
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/13 11:37
     **/
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("adversters",pxAdversterService.queryAdversterByStatus(1));
        mv.addObject("adversterSlots",pxAdversterSlotService.queryAdversterSlotByStatus(1));
        mv.setViewName("adversters/px_advert_configuration_add");
        return mv;
    }


    /**
     * @description 添加广告信息
     * @author cwj
     * @param [pxAdvertConfiguration]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/13 11:37
     **/
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView add(PxAdvertConfiguration pxAdvertConfiguration) {
        ModelAndView mv = new ModelAndView();
        pxAdversterConfigurationService.add(pxAdvertConfiguration,this.getSessionUser(),this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }


    @ResponseBody
    @RequestMapping("checkTitle")
    public Response<Object> checkName(@RequestParam(required = false) String id, String title) {
        PxAdvertConfiguration pxAdvertConfiguration = pxAdversterConfigurationService.getByTitile(title);
        if (StringUtil.isBlank(id)) {
            if (!ObjectUtils.isEmpty(pxAdvertConfiguration)) {
                return Response.failedResponse("相同的广告位已存在，请修改");
            }
        } else {
            if (!ObjectUtils.isEmpty(pxAdvertConfiguration) && !id.equals(pxAdvertConfiguration.getId())) {
                return Response.failedResponse("相同的广告位已存在，请修改");
            }
        }
        return Response.successResponse();
    }

   
    /**
     * @description 显示广告编辑页面
     * @author cwj
     * @param [id]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/13 17:30
     **/
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("adConfiguration", pxAdversterConfigurationService.findById(new ObjectId(id)));
        mv.addObject("adversters",pxAdversterService.queryAdversterByStatus(1));
        mv.addObject("adversterSlots",pxAdversterSlotService.queryAdversterSlotByStatus(1));
        mv.setViewName("adversters/px_advert_configuration_edit");
        return mv;
    }

 
    /**
     * @description 编辑广告
     * @author cwj
     * @param [pxAdvertConfiguration]
     * @return org.springframework.web.servlet.ModelAndView
     * @date @time 2025/4/13 17:48
     **/
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ModelAndView edit(PxAdvertConfiguration pxAdvertConfiguration) {
        ModelAndView mv = new ModelAndView();
        pxAdversterConfigurationService.update(pxAdvertConfiguration, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

}
